<template>
  <div id="main">
    <center-tip></center-tip>
    <router-view keep-alive v-show="showLoading"></router-view>
    <!-- <div style="text-align: center; font-size: 32px;">
      <button @click="clearCach">localStorage.clear</button>
    </div> -->
  </div>
</template>

<script>
  import { axiosGet, axiosPost } from '../assets/js/api'
  import { WxShare } from '../assets/js/share'
  import Utils from '@/assets/js/utils'
  import { Indicator } from 'mint-ui';
  import CenterTip from '@/components/CenterTip'
  export default {
    data () {
      return {
        showLoading: false
      }
    },
    mounted () {
      // oj3av1ZY30XdE7Fhdld3zYXbshSk
      // oj3av1fgufQEvqa96LwIYzQaDSyQ
      // oj3av1YR1rHDlnsUJ0b7XF2by9rc
      localStorage.setItem('agentOpenId', 'oj3av1fgufQEvqa96LwIYzQaDSyQ');
      WxShare()
      this.checkUser({
        "code": this.getCode(),
        "appId": this.getAppId()
      })
    },
    watch: {
      '$route': function (to, from) {
        this.changeTitle(to.path)
        WxShare()
      }
    },
    components: {
      CenterTip
    },
    methods: {
      changeTitle (path) {
        console.log(path)
        let title = '';
        switch (path) {
          case '/agent/invite':
            title = "招募合伙人";
            break;
          case '/agent/index':
            title = "我的权益";
            break;
          case '/agent/detail':
            title = "订单明细";
            break;
          case '/agent/record':
            title = "提现记录";
            break;
          case '/agent/partner':
            title = "我的合伙人";
            break;
          case '/agent/fans':
            title = "我的粉丝";
            break;
          case '/agent/intro':
            title = "什么是合伙人";
            break;
          case '/agent/guide':
            title = "佣金新玩法";
            break;
          default:
            title = "我的权益";
        }
        Utils.changeTitle(title)
      },
      clearCach () {
        localStorage.clear()
        alert('ok!')
      },
      closeLoading () {
        Indicator.close();
        this.showLoading = true;
      },
      getCode () {
        return Utils.getQueryString(window.location.href, 'code');
      },
      getAppId () {
        const appId = Utils.getQueryString(window.location.href, 'appId');
        window.localStorage.setItem('appId', appId)
        return appId;
      },
      checkUser (param) {
        // 针对从招募合伙人入口的逻辑处理
        if (Utils.getQueryString(window.location.href, 'type') === 'invite'){
          this.showLoading = true;
          return
        }

        Indicator.open('加载中...');

        window.localStorage.setItem('lastTime', new Date().getTime())

        let _this = this,
            nowTime = new Date().getTime(),
            lastTime = window.localStorage.getItem('lastTime') || 0,
            agentOpenId = window.localStorage.getItem('appId_' + param.appId),
            agentPicUrl = window.localStorage.getItem('agentPicUrl');

        let timeDifference = nowTime - parseFloat(lastTime);
        console.log('timeDifference:' + timeDifference/(60*1000) + '分钟')

        // 604800000 = 7*24*60*60*1000
        if (agentOpenId && parseFloat(lastTime) > 0 && timeDifference < 604800000) {
          _this.$store.commit('SET_MPOPENID', agentOpenId);
          _this.$store.commit('SET_PICURL', agentPicUrl);
          setTimeout(function () {
            _this.closeLoading();
          }, 100);
          return;
        }

        axiosGet('/api/public/1.1/h5/weixin/getUserId', {
          data: param
        })
          .then((response) => {
            if (response.success) {
              if (response.data.isAgent) {
                // 获取code和nick
                const picUrl = response.data.picUrl,
                      mpOpenId = response.data.mpOpenId;
                _this.$store.commit('SET_MPOPENID', mpOpenId);
                _this.$store.commit('SET_PICURL', picUrl);
                // window.localStorage.setItem('agentOpenId', mpOpenId);
                window.localStorage.setItem('appId_' + param.appId, mpOpenId);
                window.localStorage.setItem('agentPicUrl', picUrl);
                // 记录第一次请求时间
                window.localStorage.setItem('lastTime', new Date().getTime())
                _this.closeLoading();
              } else {
                // 没有该用户,跳转到手机绑定页面
                const mpOpenId = response.data.mpOpenId;
                window.location.replace('https://wxpre.adbaitai.com/wxpage/index.html?mpOpenId=' + mpOpenId + '&appId=' + param.appId);
              }
            } else {
              _this.closeLoading();
              alert(response.msg);
            }
          }, (error) => {
            _this.closeLoading();
            alert(response.msg);
          })
      }
    }
  }
</script>

<style lang="scss" rel="stylesheet/scss">
  #main {
    max-width: 900px;
    margin: 0 auto;
    height: 100%;
    min-height: 100%;
    background: #F5F5F5;
  }
</style>
